package com.xoa.service.party;


import com.xoa.dao.party.CommunityQfqzMapper;
import com.xoa.model.party.CommunityQfqz;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.Transient;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommunityQfqzService {

        @Autowired
        private CommunityQfqzMapper communityQfqzMapper;

    /**
     * 查询
     * @return
     */
    public ToJson select(HttpServletRequest request, CommunityQfqz communityQfqz, Integer page,
                         Integer pageSize, boolean useFlag, String export, HttpServletResponse response){
        ToJson json=new ToJson();
        Map<String,Object> map=new HashMap<String,Object>();
        PageParams params = new PageParams();
        try{
//            map.put("qfqzid",communityQfqz.getQfqzid());
//            map.put("organizationName",communityQfqz.getOrganizationName());
//            map.put("page",page);
//            map.put("pageSize",pageSize);
            params.setPageSize(pageSize);
            params.setPage(page);
            map.put("communityQfqz",communityQfqz);
            map.put("params",params);
            List<CommunityQfqz> lxdwxxcjs=communityQfqzMapper.Commnuity(map);
            if(export==null){
                export="0";
            }
            if (export.equals("1")){
                if (lxdwxxcjs !=null && lxdwxxcjs.size()>0) {
                    for (CommunityQfqz list : lxdwxxcjs){
                        if (list.getDataTime()!=null){
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String str = format.format(list.getDataTime());
                            list.setTime(str);
                        }
                    }
                }
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("群防群治组织采集", 9);
                String[] secondTitles = {"社区名称", "组织类型","组织层级", "人员数量",
                        "主要职能","地址","日期","负责人","负责人电话","业务指导部门"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"organizationName", "zzlx","zzcj", "mcount",
                        "zyzn","addresss","time","fzr","fzrtel","ywzbdt"};
                HSSFWorkbook workbook = null;
                workbook = ExcelUtil.exportExcelData(workbook2, lxdwxxcjs, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "群防群治组织采集信息记录表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }
            //int count=communityQfqzMapper.CommnuityCount(communityQfqz.getOrganizationName());
            json.setObj(lxdwxxcjs);
            json.setTotleNum(params.getTotal());
            json.setMsg("ok");
            json.setFlag(0);
            return json;
        }catch (Exception e){
            json.setMsg("ok");
            json.setFlag(0);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 修改
     * @return
     */
    @Transient
    public ToJson update(HttpServletRequest request, CommunityQfqz communityQfqz){
        ToJson json=new ToJson();
        try{
            int count=communityQfqzMapper.updateByPrimaryKey(communityQfqz);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("修改失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除
     * @return
     */
    @Transient
    public ToJson delete(Integer qfqzid){
        ToJson json=new ToJson();
        try{
            int count=communityQfqzMapper.deleteByPrimaryKey(qfqzid);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("删除失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 添加
     * @return
     */
    @Transient
    public ToJson add(HttpServletRequest request, CommunityQfqz communityQfqz){
        ToJson json=new ToJson();
        try{
            int count=communityQfqzMapper.insert(communityQfqz);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("添加失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
}
